package cn.iocoder.yudao.module.product.controller.admin.skustorerelation;

import cn.iocoder.yudao.module.product.convert.skustorerelation.SkuStoreRelationConvert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.skustorerelation.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.skustorerelation.SkuStoreRelationDO;
import cn.iocoder.yudao.module.product.service.skustorerelation.SkuStoreRelationService;

@Tag(name = "管理后台 - 门店售价")
@RestController
@RequestMapping("/product/sku-store-relation")
@Validated
public class SkuStoreRelationController {

    @Resource
    private SkuStoreRelationService skuStoreRelationService;

    @PostMapping("/createStore")
    @Operation(summary = "创建sku与适用门店关系")
    @PreAuthorize("@ss.hasPermission('product:sku-store-relation:create')")
    public CommonResult<Boolean> createStoreSkuStoreRelation(@Valid @RequestBody SkuStoreRelationSaveVO skuStoreRelationSaveVO) {
        skuStoreRelationService.createStoreSkuStoreRelation(skuStoreRelationSaveVO);
        return success(true);
    }

    @PostMapping("/updateStore")
    @Operation(summary = "跟新sku与适用门店关系")
    @PreAuthorize("@ss.hasPermission('product:sku-store-relation:update')")
    public CommonResult<Boolean> updateStoreSkuStoreRelation(@Valid @RequestBody SkuStoreRelationSaveVO skuStoreRelationSaveVO) {
        skuStoreRelationService.updateStoreSkuStoreRelation(skuStoreRelationSaveVO);
        return success(true);
    }

    @PostMapping("/createOrUpdate")
    @Operation(summary = "新增编辑门店售价")
    @PreAuthorize("@ss.hasPermission('product:sku-store-relation:create')")
    public CommonResult<Boolean> createSkuStoreRelation(@Valid @RequestBody List<SkuStoreRelationSaveReqVO> createReqVO) {
        skuStoreRelationService.createOrUpdateSkuStoreRelation(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新sku与门店关系")
    @PreAuthorize("@ss.hasPermission('product:sku-store-relation:update')")
    public CommonResult<Boolean> updateSkuStoreRelation(@Valid @RequestBody SkuStoreRelationSaveReqVO updateReqVO) {
        skuStoreRelationService.updateSkuStoreRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除sku与门店关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:sku-store-relation:delete')")
    public CommonResult<Boolean> deleteSkuStoreRelation(@RequestParam("id") Long id) {
        skuStoreRelationService.deleteSkuStoreRelation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得sku与门店关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:sku-store-relation:query')")
    public CommonResult<SkuStoreRelationRespVO> getSkuStoreRelation(@RequestParam("id") Long id) {
        SkuStoreRelationDO skuStoreRelation = skuStoreRelationService.getSkuStoreRelation(id);
        return success(BeanUtils.toBean(skuStoreRelation, SkuStoreRelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得sku与门店关系分页")
    @PreAuthorize("@ss.hasPermission('product:sku-store-relation:query')")
    public CommonResult<PageResult<SkuStoreRelationPageReqVO>> getSkuStoreRelationPage(@Valid SkuStoreRelationPageReqVO pageReqVO) {
        PageResult<SkuStoreRelationPageReqVO> pageResult = skuStoreRelationService.getSkuStoreRelationPage(pageReqVO);
        return success(pageResult);
    }

    @PostMapping("/list")
    @Operation(summary = "获得sku与门店关系列表")
    @PreAuthorize("@ss.hasPermission('product:sku-store-relation:query')")
    public CommonResult<List<SkuStoreRelationRespVO>> getSkuStoreRelationList(@Valid @RequestBody SkuStoreRelationRespVO pageReqVO) {
        List<SkuStoreRelationRespVO> skuStoreRelationDOList = skuStoreRelationService.getSkuStoreRelationList(pageReqVO);
        return success(skuStoreRelationDOList);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出sku与门店关系 Excel")
    @PreAuthorize("@ss.hasPermission('product:sku-store-relation:export')")
    @OperateLog(type = EXPORT)
    public void exportSkuStoreRelationExcel(@Valid SkuStoreRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SkuStoreRelationPageReqVO> list = skuStoreRelationService.getSkuStoreRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "sku与门店关系.xls", "数据", SkuStoreRelationRespVO.class,
                        BeanUtils.toBean(list, SkuStoreRelationRespVO.class));
    }

}